package com.maxlen.camunda.handler;

import com.maxlen.camunda.exception.CamundaException;
import com.tcloudit.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.camunda.bpm.engine.AuthorizationException;
import org.camunda.bpm.engine.BadUserRequestException;
import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.exception.NotFoundException;
import org.camunda.bpm.engine.exception.NotValidException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Arrays;

@Slf4j
@RestControllerAdvice
public class ControllerExceptionHandler {

    /**
     * 默认异常处理
     * @param exception 自定义异常
     * @return 结果
     */
    @ExceptionHandler(Throwable.class)
    public AjaxResult camundaExceptionHandler(Throwable exception){
        log.error(exception.getMessage(), exception);
        return AjaxResult.error(exception.getMessage());
    }

    /**
     * 校验参数异常处理
     * @param e 校验参数异常
     * @return 结果
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult methodArgumentNotValidException(MethodArgumentNotValidException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 自定义异常处理
     * @param exception 自定义异常
     * @return 结果
     */
    @ExceptionHandler(CamundaException.class)
    public AjaxResult camundaExceptionHandler(CamundaException exception){
        log.error(exception.getMsg(), exception);
        return AjaxResult.error(exception.getMsg());
    }

    /**
     * if there are duplicate resource names from different deployments to re-deploy.
     * @param exception NotValidException
     * @return AjaxResult
     */
    @ExceptionHandler(NotValidException.class)
    public AjaxResult notValidExceptionHandler(NotValidException exception){
        String msg = "资源发生错误";
        log.error(msg, exception);
        return AjaxResult.error(msg);
    }

    /**
     * thrown if the current user does not possess the following permissions:
     * @param exception AuthorizationException
     * @return AjaxResult
     */
    @ExceptionHandler(AuthorizationException.class)
    public AjaxResult authorizationExceptionHandler(AuthorizationException exception){
        String msg = "当前角色没有操作权限";
        log.error(msg, exception);
        return AjaxResult.error(msg);
    }

    /**
     * When a maximum results limit is specified. A maximum results
     * limit can be specified with the process engine configuration property
     * queryMaxResultsLimit (default Integer.MAX_VALUE). Please use listPage(int, int) instead.
     * @param exception BadUserRequestException
     * @return AjaxResult
     */
    @ExceptionHandler(BadUserRequestException.class)
    public AjaxResult badUserRequestExceptionHandler(BadUserRequestException exception){
        String msg = "超出限制";
        log.error(msg, exception);
        return AjaxResult.error(msg);
    }

    /**
     * when no process instance is found with a given processInstanceId.
     * limit can be specified with the process engine configuration property
     * queryMaxResultsLimit (default Integer.MAX_VALUE). Please use listPage(int, int) instead.
     * @param exception NotFoundException
     * @return AjaxResult
     */
    @ExceptionHandler(NotFoundException.class)
    public AjaxResult notFoundExceptionHandler(NotFoundException exception){
        String msg = "找不到流程定义";
        log.error(msg, exception);
        return AjaxResult.error(msg);
    }

    /**
     * If no such processDefinition can be found.
     * @param exception ProcessEngineException
     * @return AjaxResult
     */
    @ExceptionHandler(ProcessEngineException.class)
    public AjaxResult processEngineExceptionHandler(ProcessEngineException exception){
        String msg = "流程不存在";
        log.error(msg, exception);
        return AjaxResult.error(msg);
    }
}
